﻿/****************************************************************************
** file: ToupCamCamera.h
** brief: 杭州图谱光电科技有限公司 显微镜相机支持代码
** Copyright (C) LiYuan
** Author: LiYuan
** E-Mail: 18069211#qq(.)com
** Version 0.0.1
** Last modified: 2019.07.20
** Modified By: LiYuan
****************************************************************************/

#ifndef FLUOCAM_CAMERA_H
#define FLUOCAM_CAMERA_H

///
/// 杭州图谱光电科技有限公司 显微镜相机的支持代码。
///
#include "./sdk/inc/fluocam.h"
#include "CameraCommon/IndustryCameraInfo.h"
#include "CameraCommon/IndustryCamera.h"
#include "DialogCameraSetup.h"
//#include "CameraFluoSettingDialog.h"
#include <QRect>
#include <QString>
#include <QSettings>

namespace Qly
{
class CameraFluoSettingDialog;

class CAMERA_EXPORT CameraFluoImageProcessing : public CameraImageProcessing
{
    friend class CameraFluo;
    public:
    bool setWhiteBalanceMode(WhiteBalanceMode mode) Q_DECL_OVERRIDE;///
//    bool autoWhiteBalance(bool &on);
    bool whiteBalanceMode(WhiteBalanceMode &mode) Q_DECL_OVERRIDE;
    bool setManualWhiteBalance(double colorTemperature, double Tint) Q_DECL_OVERRIDE;
    bool manualWhiteBalance(double &colorTemperature, double &Tint) Q_DECL_OVERRIDE;
    bool setWhiteBalance(int red, int green, int blue) Q_DECL_OVERRIDE;
    bool whiteBalance(int &red, int &green, int &blue) Q_DECL_OVERRIDE;

    bool setGain(double fValue) Q_DECL_OVERRIDE;//
    bool gain(double &fValue) Q_DECL_OVERRIDE;//
    bool gainRange(double &fMin, double &fMax, double &fValue) Q_DECL_OVERRIDE;//
    bool setGainMode(GainMode mode)  Q_DECL_OVERRIDE;//
    bool gainMode(GainMode &mode) Q_DECL_OVERRIDE;

    bool brightnessEnable(bool &on) Q_DECL_OVERRIDE;
    bool setBrightnessEnable(bool on) Q_DECL_OVERRIDE;
    bool setBrightness(double value) Q_DECL_OVERRIDE;//
    bool brightness(double &nValue) Q_DECL_OVERRIDE;//
    bool brightnessRange(int &nMin, int &nMax, int &nValue);//

    bool setGamma(double v) Q_DECL_OVERRIDE;//
    bool gamma(double &v) Q_DECL_OVERRIDE;//
    bool gammaEnable(bool &on) Q_DECL_OVERRIDE;
    bool setGammaEnable(bool on) Q_DECL_OVERRIDE;
    bool gammaRange(double &fMin, double &fMax, double &fValue) Q_DECL_OVERRIDE;//

//    virtual bool setBlackLevel(double value); //between -1.0 and 1.0, with a default of 0.
//    virtual bool blackLevel(double &value);
//    virtual bool blackLevelEnable(bool &on);
//    virtual bool setBlackLevelEnable(bool on);

    bool setContrast(double nValue) Q_DECL_OVERRIDE;//
    bool contrast(double &nValue)  Q_DECL_OVERRIDE;//
    bool contrastEnable(bool &on) Q_DECL_OVERRIDE;
    bool setContrastEnable(bool on) Q_DECL_OVERRIDE;

    bool setSaturation(double nValue) Q_DECL_OVERRIDE;//
    bool saturation(double &value) Q_DECL_OVERRIDE;//
    bool saturationEnable(bool &on) Q_DECL_OVERRIDE;
    bool setSaturationEnable(bool on) Q_DECL_OVERRIDE;

//    virtual bool setDenoise(double value); //between -1.0 and 1.0, with a default of 0.
//    virtual bool denoise(double &value);
//    virtual bool denoiseEnable(bool &on);
//    virtual bool setDenoiseEnable(bool on);

    bool setHue(double nValue) Q_DECL_OVERRIDE;//
    bool hue(double &value) Q_DECL_OVERRIDE;//
    bool hueEnable(bool &on) Q_DECL_OVERRIDE;
    bool setHueEnable(bool on) Q_DECL_OVERRIDE;

    bool setWhiteBalanceTempTint(int temp, int tint);
    bool whiteBalanceTempTint(int &temp, int &tint);
    bool whiteBalanceTempRange(int &nMin, int &nMax, int &nValue);
    bool whiteBalanceTintRange(int &nMin, int &nMax, int &nValue);

private:
    bool contrastRange(int &nMin, int &nMax, int &nValue);//

    bool hueRange(int &nMin, int &nMax, int &nValue);//
    bool saturationRange(int &nMin, int &nMax, int &nValue);//

private:
    HFluocam m_hDevHandle;
};

class CAMERA_EXPORT CameraFluo : public IndustryCamera
{
public:
    explicit CameraFluo(QObject *parent = nullptr);
    ~CameraFluo() Q_DECL_OVERRIDE;
    QString cameraName();
    QStringList resolution();
    bool setResolutionByIdx(int N);
    bool isLive() Q_DECL_OVERRIDE;
    bool imageSize(QSize &size, QSize &max, QSize &min);

    bool autoExposureTimeLowerLimitRange(int &nMin, int &nMax, int &nValue) Q_DECL_OVERRIDE;
    bool autoExposureTimeUpperLimitRange(int &nMin, int &nMax, int &nValue) Q_DECL_OVERRIDE;

    AVPixelFormat getBayer8Patern();
public slots:
    bool openByIdx(unsigned int index);
    bool openDefaultCamera() Q_DECL_OVERRIDE;
    bool openCameraByID(QString id);
    bool startGrabbing() Q_DECL_OVERRIDE;
    bool stopGrabbing() Q_DECL_OVERRIDE;
    bool setPixelType(AVPixelFormat type) Q_DECL_OVERRIDE;
    bool saveSettingsToFile(QString fileName) Q_DECL_OVERRIDE;
    bool loadSettingsFromFile(QString fileName) Q_DECL_OVERRIDE;
    bool saveSettings(QSettings &settings);
    bool loadSettings(QSettings &settings);
    void close() Q_DECL_OVERRIDE;

    bool setExposureTimeMode(AutoExposureMode mode) Q_DECL_OVERRIDE;
    bool exposureTimeMode(AutoExposureMode &mode) Q_DECL_OVERRIDE;

    bool setExposureTime(double us) Q_DECL_OVERRIDE;
    bool exposureTime(double &us) Q_DECL_OVERRIDE;
    bool exposureTimeRange(double &fMin, double &fMax, double &fValue) Q_DECL_OVERRIDE;
    bool showVCDPropertyPage() Q_DECL_OVERRIDE;

    bool frameRate(double &fps) Q_DECL_OVERRIDE;
    bool setFrameRate(double fps) Q_DECL_OVERRIDE;

    bool setAOIRegion(QRect rect);
    bool setAOIRegion(unsigned  int xOffset, unsigned  int yOffset, unsigned  int width, unsigned  int yHeight);
    QRect AOIRegion();
    bool setHeartBeatTimeout(int ms);
	
    bool getFlip(bool &fon, bool &hon);
	bool setVFlip(bool on);
	bool setHFlip(bool on);
private:

   // CameraFluoSettingDialog * m_dialog;
    DialogCameraSetup * m_dialog;
    HFluocam m_hDevHandle;
	QString m_name;

    bool m_isLive;
    bool saveDeviceControlSettings(QSettings &settings);
    bool saveAcquisitionControlSettings(QSettings &settings);
    bool loadAcquisitionControlSettings(QSettings &settings);
    bool saveTLayerControlSettings(QSettings &settings);
    bool saveCameraSizeSettings(QSettings &settings);
    bool loadCameraSizeSettings(QSettings &settings);
    bool savePixelFormatSettings(QSettings &settings);
    bool loadPixelFormatSettings(QSettings &settings);
    bool saveAnalogControlSettings(QSettings &settings);
    bool loadAnalogControlSettings(QSettings &settings);
    bool registerImageCallBack();
    bool registerExceptionCallBack();
    /*相机抓图回调*/
    static void __stdcall ImageCallBack(unsigned nEvent, void* pCallbackCtx);
    uint8_t * pImageBuffer;
    friend void OnEventImage(CameraFluo *camera);
    friend void OnEventSnap(CameraFluo *camera);
    friend void OnEventExpo(CameraFluo *camera);
    friend void OnEventTempTint(CameraFluo *camera);
};

} //namespace Qly
#endif // FLUOCAM_CAMERA_H
